/**
* 
*/
package 
{
	import flash.display.BitmapData;
	import flash.display.IBitmapDrawable;
	import flash.events.ContextMenuEvent;
	import flash.geom.Matrix;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import mx.controls.Alert;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.events.MoveEvent;
	import mx.events.ResizeEvent;
     
	public class Reflector extends UIComponent
	{
		private var _target: UIComponent;		

		private var _alphaGradientBitmap: BitmapData;		
		private var _sx:Number = 1;
		private var _sy:Number = 1;
		private var _type:String;
		//在全局上，定义四个栏目
	     private var menu1:ContextMenuItem;
	     private var menu2:ContextMenuItem;
	     private var menu3:ContextMenuItem;
	     private var menu4:ContextMenuItem;
	     
	     public function Reflector():void{
	     	//初始化菜单
	     	menu4 = new ContextMenuItem("原图");
	     	menu1= new ContextMenuItem("左右翻转");
		    menu2 = new ContextMenuItem("上下翻转");
		     menu3 = new ContextMenuItem("左右翻转+上下翻转");		     
		     //添加菜单监听事件
		     menu1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menudraw);
		     menu2.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menudraw);
		     menu3.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menudraw);
		     menu4.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menudraw);
		     
		     //添加子菜单
		     var MyMenu:ContextMenu = new ContextMenu();
		     MyMenu.hideBuiltInItems();		     
		     MyMenu.customItems.push(menu1);
		     MyMenu.customItems.push(menu2);
		     MyMenu.customItems.push(menu3);  
		     MyMenu.customItems.push(menu4);
		     this.contextMenu = MyMenu;
	     }
	     private function menudraw(e:ContextMenuEvent):void {
	        switch (e.target) {
	         case menu1 :
	           this.type = 'LR';
	           break;
	         case menu2 :
	        	 this.type = 'TB';
	           break;
	         case menu3 :
	          	this.type = 'LRTB';
	           break;
	          case menu4 :
	          	this.type = 'O';
	           break;
	       }
	     }
		[Bindable]
		public function get target(): UIComponent {
			return _target;
		}		
		public function set target(value: UIComponent): void {
			if (_target != null) {
				// 去掉上次操作的所有监听事件
				_target.removeEventListener(FlexEvent.UPDATE_COMPLETE, handleTargetUpdate, true);
				_target.removeEventListener(MoveEvent.MOVE, handleTargetMove);
				_target.removeEventListener(ResizeEvent.RESIZE, handleTargetResize);
				
				// 清空bitmap
				clearCachedBitmaps();
			}			
			_target = value;
			
			if (_target != null) {
				_target.addEventListener(FlexEvent.UPDATE_COMPLETE, handleTargetUpdate, true);
				_target.addEventListener(MoveEvent.MOVE, handleTargetMove);
				_target.addEventListener(ResizeEvent.RESIZE, handleTargetResize);
				invalidateDisplayList();
			}
			
		}
		
		[Bindable]
		public function get type():String {
			return _type;
		}
		
		public function set type(value:String): void {
			_type = value;
			switch(_type){
				case 'LR':
					_sx = -1;
					_sy = 1;
					break;
				case 'TB':
					_sx = 1;
					_sy = -1;
					break;	
				case 'LRTB':
					_sx = -1;
					_sy = -1;
					break;
				case 'O':
					_sx = 1;
					_sy = 1;
					break;		
			}
			_alphaGradientBitmap = null;			
			invalidateDisplayList();
		}
		
		private function handleTargetUpdate(event: FlexEvent): void {
			invalidateDisplayList();
		}
		
		private function handleTargetMove(event: MoveEvent): void {
			move(_target.x, _target.y);
		}
		
		private function handleTargetResize(event: ResizeEvent): void {
			clearCachedBitmaps();
			width = _target.width;
			height = _target.height;
			invalidateDisplayList();
		}
		
		override protected function updateDisplayList(unscaledWidth: Number, unscaledHeight: Number): void {			
			if (_target != null) {				
				createBitmaps(_target);			
			}
		}
		
		private function clearCachedBitmaps(): void {
			_alphaGradientBitmap = null;
		}
		
		private function createBitmaps(target: UIComponent): void {			
			 var tX:Number = (_sx<0)?(- target.width*_sx):0;
			 var tY:Number = (_sy<0)?(- target.height * _sy):0;
			if (_alphaGradientBitmap == null) {
				_alphaGradientBitmap = new BitmapData(target.width, target.height, true, 0x00000000);
				var invertMatrix:Matrix = new Matrix();
	            invertMatrix.scale(_sx,_sy);
	            invertMatrix.translate(tX,tY);
           		 _alphaGradientBitmap.draw(target as IBitmapDrawable,invertMatrix);
           		graphics.clear();
				graphics.beginBitmapFill(_alphaGradientBitmap);
				graphics.drawRect(0,0, target.width, target.height);
				graphics.endFill();
				_target.visible = false;
			}
		}
	}
}